// pages/recordDetail/index.js
import {
  ApiCancelOrder,
  ApiOrderInvoice,
  ApiOrderGetInvoice
} from '../../api/index'
import Dialog from '@vant/weapp/dialog/dialog';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isLoading: false,
    hasInvoice: false,
    invoiceUrl: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let val = JSON.parse(options.data)
    if (val.status == '1') {
      val.statusLa = '待支付'
    } else {
      val.statusLa = '已支付'
    }
    this.getInvoice(val.id)
    this.setData({
      detailData: val,
      loginType: wx.getStorageSync('loginType')
    })

    let a = this.computedCountDown(this.data.detailData.created_at)
    this.setData({
      countDown: a
    })
  },
  getInvoice(id){
    wx.showLoading({
      title: '加载中'
    })
    ApiOrderGetInvoice(id).then(res => {
      if(res.data) {
        this.setData({
          hasInvoice: true,
          invoiceUrl: res.data.pictureNetUrl
        })
      }
      wx.hideLoading()
    })
  },
  computedCountDown(data) {
    const givenTime = new Date(data); // UTC时间表示
    const updatedTime = new Date(givenTime.getTime() + 15 * 60 * 1000);
    // 当前时间
    const currentTime = new Date();
    // 计算时间差（毫秒）
    const timeDifference = updatedTime - currentTime;
    return timeDifference
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  //取消预约
  cancelOrder() {
    let that = this
    wx.showModal({
      title: "是否取消预约该订单？",
      success(res) {
        if (res.confirm) {
          wx.showLoading({
            title: '加载中'
          })
          ApiCancelOrder(that.data.detailData.id).then((res) => {
            if (res.code == '200') {
              wx.hideLoading()
              setTimeout(() => {
                wx.navigateBack()
              }, 500);
            }

          })
          that.setData({
            isLoading: false
          })
        }

      }
    })


  },
//开票
  invoice() {
    let that = this
    Dialog.confirm({
      title: '提示',
      message: '问诊订单以及该订单下的药品处方费用将会一并开具发票',
      confirmButtonText: '确认开票'
    }).then(() => {
        wx.showLoading({
          title: '加载中'
        })
        ApiOrderInvoice(that.data.detailData.id).then(res => {
          let url = res.data?.pictureNetUrl
          wx.hideLoading()
          wx.showModal({
            title: '将下面的地址复制至浏览器获取发票',
            content: url,
            showCancel:false,
            confirmText:'复制链接',
            success (res) {
              if (res.confirm) {
                wx.setClipboardData({
                  data: url, // 要复制的内容
                  success(res) {
                    // 显示提示信息
                    wx.showToast({
                      title: '复制成功',
                      icon: 'success',
                      duration: 2000
                    });
                  }
                });
              } else if (res.cancel) {
              }
            }
          })
        })
      })
      .catch(() => {
        // on cancel
      });
  },
  downloadInvoice() {
    let that = this
    wx.showModal({
      title: '将下面的地址复制至浏览器获取发票',
      content: that.data.invoiceUrl,
      showCancel:false,
      confirmText:'复制链接',
      success (res) {
        if (res.confirm) {
          wx.setClipboardData({
            data: that.data.invoiceUrl, // 要复制的内容
            success(res) {
              // 显示提示信息
              wx.showToast({
                title: '复制成功',
                icon: 'success',
                duration: 2000
              });
            }
          });
        } else if (res.cancel) {
        }
      }
    })
  },
  submitConsultation() {
    wx.navigateTo({
      url: `/pages/submitConsultation/index?order=${JSON.stringify(this.data.detailData)}`,
    })

  },

  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})